The present invention relates in general to object-oriented technologies and more particularly to a system and method for dynamically aggregating objects.
In object oriented programming, real world objects are modeled by software objects that have encapsulated therein special procedures and data elements. In object-oriented programming terminology, procedures are referred to as methods. To avoid having to redefine the same methods and data members for each and every occurrence of an object, object-oriented programming provides the concept of classes. An inherent structure of one or more levels of increasingly more specialized classes is created to provide templates that define the methods and variables to be included in the objects of each class. The classes at the lower levels of the inheritance structure inherit the behavior, methods, and variables of the classes above. Classes above a certain class in an inheritance structure are referred to as parent classes setting up a parent-child relationship. Therefore, an object belonging to a class is a member of that class, and contains the special behavior defined by the class. In this manner, each object is an instance of a defined class or template and the need to redefine the methods and data members for each occurrence of the object is eliminated.
One example of an object-oriented programming language is Java, developed by Sun Microsystems. To define a class in Java, the programmer creates a .java file containing the source code to define the class. The .java file is compiled to create a .class file containing the executable code to define the class. Instances of the .class file are instantiated to create an object containing data and methods defined by the .class file.
Object-oriented programming is a method of programming that abstracts a computer program into manageable sections. The key to object-oriented programming is the concept of encapsulation. Encapsulation is a method by which the subroutines, or methods, that manipulate data are combined with the declaration and storage of that data. This encapsulation prevents the data from arbitrarily being accessed by other programs"" subroutines, or objects. When an object is invoked, the associated data is available and can be manipulated by any of the methods that are defined within an object to act upon the data.
The basic component of encapsulation is a class. A class is an abstraction for a set of objects that share the same structure and behavior. An object is a single instance of a class that retains the structure and behavior of the class. Objects also contain methods that are the processes by which an object is instructed to perform some procedure or manipulation of data that it controls. Classes may also be characterized by their interface which defines the elements necessary for proper communication between objects.
Often, a programmer needs to add functionality to an existing class of objects but either does not want to change the existing .class file or does not have access to the source code and, therefore, does not have the ability to alter the source code. In addition, the programmer may not want to alter the functionality of the existing .class file since a .class file may be used in more than one application program. Therefore, it is desirable to add functionality to an existing class of objects during the execution of an application program without altering the associated source code.
From the foregoing, it may be appreciated that a need has arisen for a system and method for dynamically aggregating objects to add functionality to an existing class of objects without altering the associated source code. In accordance with the present invention, an improved system and method for dynamically aggregating objects are provided which substantially eliminate or reduce disadvantages and problems associated with conventional methods for adding functionality to an existing class of objects.
According to an embodiment of the present invention, there is provided a method where an existing object is identified as a primary object and an aggregate object is created. The primary object is linked to the aggregate object. One or more facet objects that extend a functionality of the primary object are added to the aggregate object. A facet object linked to the aggregate object that has a class that equals or extends a requested class is located. A reference to the located facet object is returned. The located facet object is invoked using the returned reference.
In another embodiment of the present invention, there is provided a system for dynamically adding functionality to existing objects in an object oriented environment that comprises an aggregate object and a primary object linked to the aggregate object. One or more facet objects are linked to the aggregate object and provide additional methods and functionality for the primary object. The aggregate object, primary object, and one or more facet objects function together as a single logical object.
The present invention provides various technical advantages over conventional methods for adding functionality to an existing class of objects. For example, one technical advantage is providing a method for adding functionality to an existing class of objects without altering associated source code. In particular, the method for dynamically aggregating objects provides a method for aggregating objects so that any object within the aggregation can utilize methods of other aggregated objects. In addition, dynamically aggregating objects provides for more straightforward design and development since programmers do not need to know the technical details of the aggregated objects. Other technical advantages may be readily apparent to one skilled in the art from the following figures, description, and claims.